﻿Public Class Form1

    Public filesListBox As New List(Of archivo)
    Public extensionList As New List(Of extension)
    Private Carpeta As String
    Private Myserver As New Servidor()

    Private Sub btnSelect_Click(sender As System.Object, e As System.EventArgs) Handles btnSelect.Click
        filesListBox.Clear()
        extensionList.Clear()
        ListBox1.Items.Clear()

        Dim result As DialogResult
        result = FolderBrowserDialog1.ShowDialog
        Carpeta = FolderBrowserDialog1.SelectedPath
        If result = Windows.Forms.DialogResult.OK Then
            ListFiles(FolderBrowserDialog1.SelectedPath)

            ContarExt()
            For Each file In extensionList
                ListBox1.Items.Add(file.ToString)
            Next

        End If
    End Sub
    Private Sub ListFiles(ByVal folderPath As String)

        Dim fileNames = My.Computer.FileSystem.GetFiles(folderPath, FileIO.SearchOption.SearchAllSubDirectories, "*.*")
        For Each fileName As String In fileNames
            Dim split() As String
            split = fileName.Split(".")
            Dim aux As archivo
            If split.Length > 1 Then
                aux = New archivo(fileName, split(1))
            Else
                aux = New archivo(fileName, "Sin extensión")
            End If
            filesListBox.Add(aux)
        Next
    End Sub

    Private Sub ContarExt()
        For Each archivoLista In filesListBox
            If Not extensionList.Contains(archivoLista.extension) Then
                extensionList.Add(archivoLista.extension)

            Else
                extensionList.Item(extensionList.IndexOf(archivoLista.extension)).numero = extensionList.Item(extensionList.IndexOf(archivoLista.extension)).numero + 1
            End If
            Next
    End Sub

    
    Private Sub btnSubir_Click(sender As System.Object, e As System.EventArgs) Handles btnSubir.Click
        Dim clienteFtp As New FTP("guest", "127.0.0.1") 'cambiar en la version final 
        AddHandler clienteFtp.SinConexion, AddressOf clienteFtp_SinConexion
        clienteFtp.Carpeta = Carpeta
        clienteFtp.Preparacion()
        clienteFtp.Conectar()

        AddHandler Myserver.DatosRecibidos, AddressOf myServer_DatosRecibidos
        Myserver.Ip = System.Net.IPAddress.Loopback.ToString
        Myserver.Puerto = 8080
        Myserver.StartListener()

    End Sub

    Private Sub clienteFtp_SinConexion(ByVal sender As Object, ByVal e As ConexionEvenArgs)
        Dim dialog As DialogResult
        dialog = MessageBox.Show("¿Ha ocurrido un Error salir?", e.Mensaje, MessageBoxButtons.YesNo, MessageBoxIcon.Error)
        If dialog = Windows.Forms.DialogResult.Yes Then
            Me.Close()
        End If
    End Sub

    Private Sub myServer_DatosRecibidos(ByVal IdTerminal As Net.IPEndPoint)
        Dim mensaje As String = Myserver.Obtener_datos(IdTerminal)
        If mensaje = "Terminado" Then
            Try
                Myserver.StopListener()
            Catch ex As Exception
            End Try
        Else
            Me.ListBox1.Items.Clear()
            Me.ListBox1.Items.Add(mensaje)
        End If
        Dim clienteFtp As New FTP("guest", "127.0.0.1") 'cambiar en la version final 
        AddHandler clienteFtp.SinConexion, AddressOf clienteFtp_SinConexion
        clienteFtp.Download()
    End Sub

End Class